package aom.ju.ss

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import aom.ju.ss.databinding.MainBinding
import java.util.*


open class BaseActivity2<T : ViewDataBinding> : AppCompatActivity() {


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        var bindingName: String

        var layName = ""

        javaClass.genericSuperclass?.toString()?.split(".")?.apply {
            bindingName = this[this.size - 1].removeSuffix("Binding>")

            bindingName.forEach {
                if (Character.isUpperCase(it) && layName.isNotEmpty()) {
                    layName += "_"
                }
                layName += it.toString()

            }
            layName = layName.toLowerCase(Locale.ROOT)
        }


        binding = DataBindingUtil.setContentView(
            this, resources.getIdentifier(
                layName, "layout",
                packageName
            )
        )


    }

    lateinit var binding: T


}


//用法
class MyActivity : BaseActivity2<MainBinding>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.tab
    }

}